<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * MAPI COA Project
 * mapi.php
 * @author ranuandra
 *
 * 22 Jul 2011
 * tags
 */

abstract class Kohana_Polreg extends Controller_Template

{
	
	public $main_template = '';
	
	public $template = 'main';
      // Controls access for the whole controller, if not set to FALSE we will only allow user roles specified
      // Can be set to a string or an array, for example array('login', 'admin') or 'login'
    public $auth_required = FALSE;
 
      // Controls access for separate actions
      // 'adminpanel' => 'admin' will only allow users with the role admin to access action_adminpanel
      // 'moderatorpanel' => array('login', 'moderator') will only allow users with the roles login and moderator to access action_moderatorpanel
    public $secure_actions = FALSE;
    
    public $action;
	
/**
      * Initialize properties before running the controller methods (actions),
      * so they are available to our action.
      */
/**
      * Initialize properties before running the controller methods (actions),
      * so they are available to our action.
      */
    
     public function before()
      {

         parent::before();
         $this->action = Request::current()->action();
         //echo Request::current()->controller();
         $action_name = Request::current()->action();
         if (($this->auth_required !== FALSE && Auth_ORM::instance()->logged_in($this->auth_required) === FALSE) || (is_array($this->secure_actions) && array_key_exists($action_name, $this->secure_actions)))
		 {
		 	
			if (Auth_ORM::instance()->logged_in()){
				Request::current()->redirect(URL::base());
				
				/**if(Member::instance()->get_user()->activated == 0)
				{
					Request::instance()->redirect(URL::base().'register/activated/'.Member::instance()->get_user()->id);

				}
				else
				{
					if(Member::instance()->get_user()->verified == 0)
					{
						Request::instance()->redirect(URL::base().'member/detail');
					}					
				}**/
			}else{
				Request::current()->redirect(URL::base());
			}
		 }
		 
		 

         if($this->auto_render)
          {
            // Initialize empty values
            $this->template->title            = '';
            $this->template->meta_keywords    = '';
            $this->template->meta_description = '';
            $this->template->meta_copywrite   = '';
            $this->template->header           = '';
            $this->template->content          = '';
            $this->template->footer           = '';
            $this->template->styles           = array();
            $this->template->scripts          = array();
            $this->template->main			  = '';
          }
      }

     /**
      * Fill in default values for our properties before rendering the output.
      */
     public function after()
      {
         if(Request::current()->is_ajax() == $this->request)
		 {
		 	$this->auto_render = false;
		 	$this->template->test          = true;
		 }	
		 else
		 {
		 	$this->auto_render = true;
		 	$this->template->test           = "";
		 }
		 
         if($this->auto_render)
          {
             // Define defaults
             $styles                  = array(URL::base().'assets/css/reset.css' => 'screen');
             $scripts                 = array(URL::base().'assets/js/jquery-1.4.2.min.js');

             // Add defaults to template variables.
             $this->template->styles  = array_reverse(array_merge($this->template->styles, $styles));
             $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts));
             $this->template->ref = "";
            
           }


         // Run anything that needs to run after this.
         parent::after();
      } 

      public function xlstoarry($data)
      {
			$cols = array();
			$k = 1;
			$datas = array();
			$jml = 1;
      		
			for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) 
			{
				//echo $i."<br/>";
				if($data->sheets[0]['cells'][$i][1]!="") // cek coloumn pertama apakah ada isinya
				{
					if($k==1)//bila itu row pertama maka ambil no column
					{
						if($data->sheets[0]['cells'][$i][1]=="area")
						{
							for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
								//echo "\"".$data->sheets[0]['cells'][$i][$j]."\",";
								$cols[$data->sheets[0]['cells'][$i][$j]] = $j;
								
							}
							$k = 0;
						}
						//print_r($cols);
						//exit;
					}
					else //masukkan data
					{
						//echo "dasdasd";
						//$k = 0;
						
						$area = $data->sheets[0]['cells'][$i][$cols['area']];
						$nama = $data->sheets[0]['cells'][$i][$cols['nama']];
						$alamat = $data->sheets[0]['cells'][$i][$cols['alamat']];
						$model = $data->sheets[0]['cells'][$i][$cols['jenis/model']];
						$merk = $data->sheets[0]['cells'][$i][$cols['merek']];
						$type = $data->sheets[0]['cells'][$i][$cols['type']];
						$cc = str_replace("'", "", $data->sheets[0]['cells'][$i][$cols['cc']]);
						
						if($data->sheets[0]['cells'][$i][$cols['no_rangka']]!="")
						{
							$noka = $data->sheets[0]['cells'][$i][$cols['no_rangka']];
						}
						else if($data->sheets[0]['cells'][$i][$cols['noka']]!="")
						{
							$noka = $data->sheets[0]['cells'][$i][$cols['noka']];
						}
						
						if($data->sheets[0]['cells'][$i][$cols['no_mesin']]!="")
						{
							$nosin = $data->sheets[0]['cells'][$i][$cols['no_mesin']];
						}
						else if($nosin = $data->sheets[0]['cells'][$i][$cols['nosin']]!="")
						{
							$nosin = $data->sheets[0]['cells'][$i][$cols['nosin']];
						}
						
						if($data->sheets[0]['cells'][$i][$cols['thn']]!="")
						{
							$thn = str_replace("'", "", $data->sheets[0]['cells'][$i][$cols['thn']]);
						}
						elseif($data->sheets[0]['cells'][$i][$cols['thn buat']]!="") 
						{
							$thn = str_replace("'", "", $data->sheets[0]['cells'][$i][$cols['thn buat']]);
						}
						elseif($data->sheets[0]['cells'][$i][$cols['thn_buat']]!="") 
						{
							$thn = str_replace("'", "", $data->sheets[0]['cells'][$i][$cols['thn_buat']]);
						}
						
						//$tgl = date("Y-m-d H:m:s", strtotime($data->sheets[0]['cells'][$i][$cols['tgl_daftar']]));
						if($data->sheets[0]['cells'][$i][$cols['tgl_daftar']]!="")
						{
							$tgl_asli = $data->sheets[0]['cells'][$i][$cols['tgl_daftar']];
						//echo $tgl_asli;
						}
						elseif($data->sheets[0]['cells'][$i][$cols['tgl daftar']]!="")
						{
							$tgl_asli = $data->sheets[0]['cells'][$i][$cols['tgl daftar']];
						}
						
						if($tgl_asli == "")
						{
							$tgl_asli = '15/'.Request::current()->post('bulan', null).'/'.Request::current()->post('tahun', null);
							//$tgl_asli = $_POST['tahun'].'/'.$_POST['bulan'].'/10';
						}
						
						$jam = "00:00:00";
						
						if(strpos($tgl_asli, '/') == true)
						{
							list($d, $m, $y) = preg_split('/\//', $tgl_asli);
						}
						else 
						{
							list($d, $m, $y) =  explode('-', $tgl_asli);
						}
						
						$tgl = $y."/".$m."/".$d." ".$jam;
						
						if($noka!="") //bila terdapat no rangka maka masukkan ke data
						{				
							$datas[] = array(	'area'		=>	$area, 
													 'nama'		=>	$nama, 
													 'alamat'	=>	$alamat, 
													 'model'	=>	$model, 
													 'merk'		=>	$merk, 
													 'type'		=>	$type,
													 'cc'		=> 	$cc,
													 'noka'		=>	$noka,
													 'nosin'	=> 	$nosin,
													 'thn'		=> 	$thn,
													 'tgl'		=> 	$tgl
												);
						}	
					}
				}
			}
			//print_r($datas);

			return $datas;
      }

}
?>